<template>
  <div class="app-container" v-wechat-title="'分类列表'">
    <!-- 搜索表单 -->
    <el-form
        ref="queryForm"
        :model="queryParams"
        size="small"
        :inline="true"
        @submit.native.prevent
    >
      <el-form-item>
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd" :disabled="isAuth('sys:category:add')">新增</el-button>
        <el-button type="danger" icon="el-icon-delete" :disabled="multiple || isAuth('sys:category:delete')" @click="handleDelete">删除</el-button>
      </el-form-item>
      <el-form-item>
        <el-input
            v-model="queryParams.name"
            placeholder="分类名字"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item >
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table :data="pageList"
              border
              style="max-width: 1296px"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column
          align="center"
          prop="name"
          label="分类名称"
          >
      </el-table-column>
      <el-table-column
          align="center"
          label="分类描述"
          >
        <template slot-scope="scope">
          <el-tag type="info" size="mini" effect="plain" v-if="scope.row.description == null">暂无描述</el-tag>
          {{scope.row.description}}
        </template>
      </el-table-column>
<!--      <el-table-column label="图片" width="400" align="center">-->
<!--        <template slot-scope="scope">-->
<!--          &lt;!&ndash;          <pan-thumb :image="scope.row.picture" :height="'100px'" :width="'100px'">&ndash;&gt;-->
<!--          <span style="margin-left: 9px">  嘿嘿！</span>-->
<!--          <svg-icon icon-class="emoji-25" style="width: 50px;height: 42px"></svg-icon>-->
<!--          &lt;!&ndash;          </pan-thumb>&ndash;&gt;-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column
          prop="createTime"
          label="创建时间"
          align="center"
          >
      </el-table-column>
      <el-table-column
          prop="modifiedTime"
          label="修改时间"
          align="center"
          >
        <template slot-scope="scope">{{ scope.row.modifiedTime == null ? '暂无修改' :scope.row.modifiedTime}}</template>
      </el-table-column>
      <el-table-column label="操作" align="center" >
        <template slot-scope="scope">
          <el-button
              type="primary"
              icon="el-icon-edit"
              size="medium"
              circle
              plain
              :disabled="isAuth('sys:category:update')"
              @click.stop="handleUpdate(scope.row)"
          />

          <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              circle
              plain
              :disabled="isAuth('sys:category:delete')"
              @click.stop="handleDelete(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="pagination.total>0"
        :total="pagination.total"
        :page.sync="pagination.currentPage"
        :limit.sync="pagination.pageSize"
        @pagination="handleQuery"
    />

    <!-- 表单弹窗 -->
    <el-dialog :title="dialog.title" :visible.sync="dialog.visible" width="700px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" @submit.native.prevent>
        <el-form-item label="分类名" prop="name">
          <el-input @keyup.enter.native="handleSubmit" v-model="form.name" placeholder="请输入分类名"/>
        </el-form-item>
        <el-form-item label="分类描述">
          <el-input @keyup.enter.native="handleSubmit" v-model="form.description" placeholder="填写分类描述"/>
        </el-form-item>
        <!--        <el-form-item label="分类图片" prop="clientSecret">-->
        <!--          <el-input v-model="form.picture" placeholder="请输入客户端密钥"/>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="dialog.visible=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {page, detail, update, add, del} from '@/api/category'

export default {
  data() {
    return {
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      queryParams: {
        name: undefined
      },
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      pageList: [],
      dialog: {
        title: undefined,
        visible: false,
        type: undefined
      },
      // 表单参数
      form: {
        id: '',
        name: '',
        picture: ''
      },
      // 表单校验
      rules: {
        name: [
          {required: true, message: '分类名不能为空', trigger: 'blur'}
        ],
      }
    }
  },
  created() {
    this.handleQuery()
  },
  methods: {
    handleQuery() {
      this.queryParams.currentPage = this.pagination.currentPage
      this.queryParams.pageSize = this.pagination.pageSize
      page(this.queryParams).then(res => {
        this.pageList = res.data.records
        this.pagination.total = res.data.total
        this.pagination.currentPage = res.data.current
        this.pagination.pageSize = res.data.size
      })
    },
    handleReset() {
      this.pagination = {
        currentPage: 1,
        pageSize: 10,
        total: 0
      }
      this.queryParams = {
        name: undefined
      }
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    handleAdd() {
      this.resetForm()
      this.dialog = {
        title: '新增分类',
        visible: true,
        type: 'add'
      }
    },
    handleUpdate(row) {
      this.resetForm()
      this.dialog = {
        title: '修改分类',
        visible: true,
        type: 'edit'
      }
      this.form.id = row.id
      detail(this.form.id).then(response => {
        const {data} = response
        this.form = data
        if (data.authorizedGrantTypes) {
          this.form.authorizedGrantTypes = data.authorizedGrantTypes.split(',')
        }
      })
    },
    handleDelete(row) {
      const ids = [row.id || this.ids].join(',')
      this.$confirm('确认删除已选中的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(ids).then(() => {
          this.$message.success('删除成功')
          this.handleQuery()
        })
      }).catch(() => {
      })
    },
    handleSubmit: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id) {
            update(this.form).then(() => {
              this.$message.success('修改成功')
              this.closeDialog()
              this.handleQuery()
            })
          } else {
            add(this.form).then(() => {
              this.$message.success('新增成功')
              this.closeDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetForm() {
      this.form = {}
      if (this.$refs['form']) {
        this.$refs['form'].resetFields()
      }
    },
    closeDialog() {
      this.dialog = {
        title: '',
        visible: false,
        type: ''
      }
      this.form.id = ''
    }
  }
}
</script>
